package com.saikei.statstitlebar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * @Author:gewu
 * @Time:2022/10/2119:32
 * @Description:状态栏工具
 */
public class TitleUtil {

    private static Activity getActivity(Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else {
            return null;
        }
    }

    /**
     * @Author:gewu
     * @Time:2022/10/2119:28
     * @Description:安卓6.0设置状态栏颜色
     */
    public static void setStatusBarTransparent(Context context, int statusBarColor) {
        Window window = getActivity(context).getWindow();
        View decorView = window.getDecorView();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            window.setStatusBarColor(Color.TRANSPARENT);
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int gray = (int) (Color.red(statusBarColor) * 0.299 + Color.green(statusBarColor) * 0.587 + Color.blue(statusBarColor) * 0.114);
            if (gray >= 192) {
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            }
        }
    }

    public static boolean getRgb(int color){
        return ((int) (Color.red(color) * 0.299 + Color.green(color) * 0.587 + Color.blue(color) * 0.114)) >= 192;
    }


    /**
     * @Author:gewu
     * @Time:2022/10/2119:30
     * @Description:获取状态栏高度
     * @param context
     */
    public static int getStatusBarHeight(Context context) {
        int identifier = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(identifier);
    }

}
